﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace Projekt_GUI.Forms
{
    public partial class BuchungUebersicht : Form
    {
        OracleHelper oracle { get { return OracleHelper.Instance; } }
        public BuchungUebersicht()
        {
            InitializeComponent();
        }

        private void BuchungUebersicht_Load(object sender, EventArgs e)
        {
            Refresh();
        }
        private void Refresh()
        {
            oracle.Open();
            OracleDataReader r = oracle.Select("Reise, Kunde, Rabatt, Mitarbeiter, Nummer", "Buchungen");
            int counter = 0;
            listView1.Items.Clear();
            while (r.Read())
            {
                  listView1.Items.Add(r.GetValue(0).ToString());
                  listView1.Items[counter].SubItems.Add(r.GetValue(1).ToString());
                  listView1.Items[counter].SubItems.Add(r.GetValue(2).ToString());
                  listView1.Items[counter].SubItems.Add(r.GetValue(3).ToString());
                  listView1.Items[counter].SubItems.Add(r.GetValue(4).ToString());
                  counter++;
            }
                
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    using (OracleConnection con = new OracleConnection("Data Source=localhost; User Id=PROJEKT; Password=PROJEKT"))
                    {
                        con.Open();
                        OracleCommand com = con.CreateCommand();
                        com.CommandText = "DELETE FROM Buchungen WHERE Nummer = " + listView1.SelectedItems[0].SubItems[4].Text + "";

                        com.ExecuteNonQuery();
                    }
                }
                catch (OracleException ex)
                {
                    MessageBox.Show(ex.Message, "Oracle Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                Refresh();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
